package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.mchCreateCsBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.mchCreateCsCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.cs_info;
import com.ymttest.database.model.godmch.buyer_invite_code;
import com.ymttest.database.model.godmch.buyer_invite_codeExample;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.buyer_invite_codeWapper;
import com.ymttest.utils.TestDataManager;
/**
*商户主动创建客服
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_mchCreateCs{
	private static mchCreateCsBean bean;
	private static mchCreateCsCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商户主动创建客服");
	}

	@Before
	public void caseUp() {
		bean=new mchCreateCsBean();
		call=new mchCreateCsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	 private void check_invitecode(Integer csId) throws InterruptedException {

		buyer_invite_codeExample example=new buyer_invite_codeExample();
		example.createCriteria().andUserIdEqualTo(csId);
		buyer_invite_codeWapper codeMapper=new buyer_invite_codeWapper();
		List<buyer_invite_code> codes=codeMapper.selectByExample(example);	
		Logger.verifyEquals(1, codes.size(), "验证邀请码表新增一条数据");
		Logger.verifyEquals(2, codes.get(0).getUserType(), "验证UserType=2");
		Logger.verifyEquals(0, codes.get(0).getCodeStatus(), "验证CodeStatus=0");

	}
	 
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_001() {
		Logger.start(true,"正例-商户主动创建客服");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title");
			bean.setLoginId("商户主动创建客服Loginid_"+TestDataManager.getRandomIntNum(5));
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();				
			
			cs_info csi=new DefGodMchWapper().selectCsInfoByMchId(bean.getMchId()).get(0);
			Logger.verifyEquals("200", call.getString("status"), "验证status");

			Logger.verifyEquals(bean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(bean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(bean.getTitle(), csi.getTitle(), "验证Title");
			
			Logger.verifyEquals(0, csi.getState(), "验证State");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("create_cs-"+csi.getMchId()+"-"+csi.getCsId(),"cs_add");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(csi.getMchId().toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			Logger.verifyEquals(csi.getCsId().toString(), joa.get(0).get("csId").toString(), "验证总线消息csId");
			
			check_invitecode(csi.getCsId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_001_1() {
		Logger.start(true,"正例-独立商户主动创建客服");
		try {
			createIndepMchBean createmchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createmchBean);
			
			bean.setMchId(createIndepMchCall.getmchId());
			bean.setTitle("独立商户主动创建客服title");
			bean.setLoginId("独立商户主动创建客服_"+TestDataManager.getRandomIntNum(5));
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createIndepMchCall.getmchId());

			call.setData(bean);
			call.callService();				
			
			cs_info csi=new DefGodMchWapper().selectCsInfoByMchId(bean.getMchId()).get(0);
			Logger.verifyEquals("200", call.getString("status"), "验证status");

			Logger.verifyEquals(bean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(bean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(bean.getTitle(), csi.getTitle(), "验证Title");
			
			Logger.verifyEquals(0, csi.getState(), "验证State");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("create_cs-"+csi.getMchId()+"-"+csi.getCsId(),"cs_add");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(csi.getMchId().toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			Logger.verifyEquals(csi.getCsId().toString(), joa.get(0).get("csId").toString(), "验证总线消息csId");
			
			check_invitecode(csi.getCsId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_002() {
		Logger.start(true,"正例-商户主动创建客服-客服名称为空");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title");
			//bean.setLoginId("商户主动创建客服_"+TestDataManager.getRandomIntNum(5));
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_003() {
		Logger.start(true,"正例-商户主动创建客服-客服昵称重复");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title");
			bean.setLoginId("商户主动创建客服_"+TestDataManager.getRandomIntNum(5));
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();	
			call.callService();
	
			Logger.verifyEquals("5000", call.getString("status"), "验证status");
			Logger.verifyEquals(true, call.getString("msg").contains("客服昵称已存在"), "验证status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_003_1() {
		Logger.start(true,"正例-商户主动创建客服-LoginId重复");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();	
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();
	
			Logger.verifyEquals("5000", call.getString("status"), "验证status");
			//Logger.verifyEquals(true, call.getString("msg").contains("客服昵称已存在"), "验证status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_mchCreateCs_004() {
		Logger.start(true,"正例-商户主动创建客服-password不符合规则-密码可设置6-10位,数字+英文组合(目前godmch没有对password做规则验证)");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title");
			bean.setLoginId("商户主动创建客服_"+TestDataManager.getRandomIntNum(5));
			bean.setPassword("111");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_005() {
		Logger.start(true,"正例-商户主动创建客服-templateId不存在");
		try {

			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1111 );
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5022", call.getString("status"), "验证status");
			Logger.verifyEquals(true, call.getString("msg").contains("客服角色不存在"), "验证msg");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_006() {
		Logger.start(true,"正例-商户主动创建客服-templateId为空");
		try {

			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(null);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_007() {
		Logger.start(true,"正例-商户主动创建客服-mchId不存在");
		try {

			bean.setMchId(999);
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+999);

			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("5004", call.getString("status"), "验证status");
			Logger.verifyEquals(true, call.getString("msg").contains("商户不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_008() {
		Logger.start(true,"正例-商户主动创建客服-mchId为空");
		try {

			bean.setMchId(null);
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_");

			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_009() {
		Logger.start(true,"正例-商户主动创建客服-channelSource为空");
		try {
			
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(null);
			bean.setTerminalSource(0);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_010() {
		Logger.start(true,"正例-商户主动创建客服-terminalSource为空");
		try {

			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(null);
			bean.setAppSource(60);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchCreateCs_011() {
		Logger.start(true,"正例-商户主动创建客服-appSource为空");
		try {
			
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			bean.setMchId(createmchBean.getMchId());
			bean.setTitle("商户主动创建客服title_"+TestDataManager.getRandomIntNum(5));
			bean.setLoginId("商户主动创建客服123");
			bean.setPassword("111aaa");
			bean.setTemplateId(1);
			bean.setChannelSource(1);
			bean.setTerminalSource(0);
			bean.setAppSource(null);
			bean.setAppKey("pcsqseller_"+createmchBean.getMchId());

			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}